/*
*********************************************************************************************************
*                                     MICIRUM BOARD SUPPORT PACKAGE
*
*                          (c) Copyright 2003-2009; Micrium, Inc.; Weston, FL
*
*               All rights reserved.  Protected by international copyright laws.
*               Knowledge of the source code may NOT be used to develop a similar product.
*               Please help us continue to provide the Embedded community with the finest
*               software available.  Your honesty is greatly appreciated.
*********************************************************************************************************
*/

/*
*********************************************************************************************************
*
*                                        BOARD SUPPORT PACKAGE
*
*                                        Renesas SH-2AFPU 7216
*                                               on the
*                                      R0K7216 Evaluation Board
*
* Filename      : bsp.c
* Version       : V1.00
* Programmer(s) : HMS
*********************************************************************************************************
*/

#include <include.h>

/*
*********************************************************************************************************
*                                               CONSTANTS                                                
*********************************************************************************************************
*/

/*
*********************************************************************************************************
*                                            GLOBAL VARIABLES
*********************************************************************************************************
*/

extern APP_INTTABLE_ELEM  __vector_table[];
void SparkTimer1ISR( void );
void SparkTimer2ISR( void );
/*
*********************************************************************************************************
*                                               PROTOTYPES
*********************************************************************************************************
*/

static void  Tmr_TickInit      (void);

/*
*********************************************************************************************************
*                                           BSP INITIALIZATION
*
* Description : This function should be called by your application code before you make use of any of the
*               functions found in this module.
*
* Arguments   : none
*********************************************************************************************************
*/

void  BSP_Init (void)
{
  Tmr_TickInit();                                                     /* Initialize uC/OS-II's tick ISR                   */
  //BSP_VectAdd(SparkTimer1ISR,ATUC0_IMIC00); /* Register interrupt to vector table */
  //BSP_VectAdd(SparkTimer2ISR,ATUC0_IMIC01);
}

/*
*********************************************************************************************************
*                                           BSP INITIALIZATION
*
* Description : This function should be called by your application code before you make use of any of the
*               functions found in this module.
*
* Arguments   : none
*********************************************************************************************************
*/

void  BSP_VectAdd (CPU_FNCT_VOID p_func, CPU_INT08U vect)
{
  __vector_table[vect - 4].Fnct = p_func;
}

/*
*********************************************************************************************************
*                                        TICKER INITIALIZATION
*
* Description   : This function initializes the uC/OS-II's tick source.
*
* Argument      : None
*
* Returns       : None
*********************************************************************************************************
*/

void  Tmr_TickInit (void)
{
    CPU_INT16U  tmp_cmcor;
        
    CMT.CMSTR.BIT.STR0   = 0;                                           /* Stop timer channel 0  */
    CMT.Channel[0].CMCR.BIT.CKS   = 0;                                           /* Clock counts on Pclk / 8  */
    tmp_cmcor = (BSP_CPU_CLKFREQ / OS_TICKS_PER_SEC) / 8;
    CMT.Channel[0].CMCOR          = tmp_cmcor;
    CMT.Channel[0].CMCR.BIT.CMIE  = 1;                                           /* Enable interrupts */
    CMT.CMSTR.BIT.STR0   = 1;                                           /* Start the timer */
    INTC.IPR05.BIT.IPL_CMT0 = OS_INTERRUPT_PRIORITY;                       /* Set interrupt priority */
}
/*
*********************************************************************************************************
*                                          ISR TICK CLEAR
*
* Description   : This function is part of the ISR for the OSTick.
*
* Argument      : None
*
* Returns       : None
*********************************************************************************************************
*/

void  TickClr (void)
{
  CMT.Channel[0].CMSR.BIT.CMF = 0;
  
#if (1 == TEST_PIN_EN) && (1 == TICK_TEST_EN)
  testPin = ~testPinState;
#endif
}

/**************************************************************************
 *Function   :  TimerInit     
 *Description:  Init The ATU-III                                                   
 *Input      :                             
 *Output     :                              
 *Return     : 
 *G Variable :                              
 *Notes      :                                   
 **************************************************************************/
void TimerInit( void )
{
  //ATU Common Control Init
  ATUCTRL.PSCR0.BIT.PSC0  = ATU_CLK0_DIV; //Prescaler Registers 0
  ATUCTRL.PSCR1.BIT.PSC1  = ATU_CLK1_DIV; //Prescaler Registers 0
  ATUCTRL.PSCR2.BIT.PSC2  = ATU_CLK2_DIV; //Prescaler Registers 0
  ATUCTRL.PSCR3.BIT.PSC3  = ATU_CLK3_DIV; //Prescaler Registers 0
  ATUCTRL.ATUENR.BIT.PSCE = 1; //ATU-III Master Enable Register
  ATUCTRL.CBCNT.BIT.CB4EG = 0; //Clock Bus Control Register
  ATUCTRL.CBCNT.BIT.CB5SEL = 1; //Multiplied-and-corrected clock output by Timer B
  ATUCTRL.NCMR.BYTE       = 0; //Noise Cancellation Mode Register
  
  /* TimerE Init */
  TimerE_Init();
}